using AForge.Video.DirectShow;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _调用USB摄像头
{
public partial class Form1 : Form
{
private FilterInfoCollection videoDevices;//所有摄像设备
private VideoCaptureDevice videoDevice;//摄像设备
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);//得到所有接入的摄像设备
if (videoDevices.Count != 0)
{
foreach (FilterInfo device in videoDevices)
{
comboBox1.Items.Add(device.Name);//把摄像设备添加到摄像列表中
}
}
else
{
MessageBox.Show("没有找到摄像头!");
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
videoDevice = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
videoSourcePlayer1.VideoSource = videoDevice;
videoSourcePlayer1.SignalToStop();//使用方法等待视频源停止。
videoSourcePlayer1.WaitForStop();//等待视频源停止或暂停
}
private void uiButton1_Click(object sender, EventArgs e)
{
videoSourcePlayer1.Start();//开始
}
private void uiButton2_Click(object sender, EventArgs e)
{
videoSourcePlayer1.Stop();//暂停
}
}
}
评论